| Conditions | 17 |
| Paths | 8640 |
| Total Lines | 429 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like $(document).ready often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | var g_chosen_datetimes = []; |
||
| 204 | $(document).ready(function () { |
||
| 205 | // enable / disable date picker |
||
| 206 | var i; |
||
| 207 | $('#id_expire_set').click(function(){ |
||
| 208 | $('#id_expire_date').prop("disabled", !this.checked); |
||
| 209 | if (this.checked) { |
||
| 210 | $("#id_expire_date").focus(); |
||
| 211 | } |
||
| 212 | }); |
||
| 213 | |||
| 214 | var anonOptions = document.getElementById('anonOptions'); |
||
| 215 | $('#hideNames').click(function() { |
||
| 216 | hideNames = this.checked; |
||
| 217 | }); |
||
| 218 | |||
| 219 | $('#isAnonymous').click(function() { |
||
| 220 | isAnonymous = this.checked; |
||
| 221 | if (isAnonymous) { |
||
| 222 | anonOptions.style.display = 'inline'; |
||
| 223 | } else { |
||
| 224 | anonOptions.style.display = 'none'; |
||
| 225 | } |
||
| 226 | }); |
||
| 227 | |||
| 228 | var privateRadio = document.getElementById('private'); |
||
| 229 | var hiddenRadio = document.getElementById('hidden'); |
||
| 230 | var publicRadio = document.getElementById('public'); |
||
| 231 | var selectRadio = document.getElementById('select'); |
||
| 232 | if (privateRadio.checked) { |
||
| 233 | access_type = 'registered'; |
||
| 234 | } else if (hiddenRadio.checked) { |
||
| 235 | access_type = 'hidden'; |
||
| 236 | } else if (publicRadio.checked) { |
||
| 237 | access_type = 'public'; |
||
| 238 | } else if (selectRadio.checked) { |
||
| 239 | access_type = 'select'; |
||
| 240 | } |
||
| 241 | |||
| 242 | isAnonymous = document.getElementById('isAnonymous').checked; |
||
| 243 | hideNames = anonOptions.checked; |
||
| 244 | |||
| 245 | var accessValues = document.getElementById('accessValues'); |
||
| 246 | if (accessValues.value.length > 0) { |
||
| 247 | var list = document.getElementById('selected-search-list-id'); |
||
| 248 | var accessValueArr = accessValues.value.split(';'); |
||
| 249 | for (i=0; i<accessValueArr.length; i++) { |
||
| 250 | var val = accessValueArr[i]; |
||
| 251 | if (val === '') { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | var li = document.createElement('li'); |
||
| 255 | li.id = val; |
||
| 256 | li.className = 'cl_item cl_access_item selected'; |
||
| 257 | var index = val.indexOf('group_'); |
||
| 258 | if (index === 0) { |
||
| 259 | g_chosen_groups.push(val); |
||
| 260 | li.className += ' is-group'; |
||
| 261 | li.appendChild(document.createTextNode(val.substring(6) + " (group)")); |
||
| 262 | list.appendChild(li); |
||
| 263 | } else { |
||
| 264 | index = val.indexOf('user_'); |
||
| 265 | if (index === 0) { |
||
| 266 | g_chosen_users.push(val); |
||
| 267 | li.className = 'cl_item cl_access_item selected'; |
||
| 268 | var username = val.substring(5); |
||
| 269 | $.post(OC.generateUrl('/apps/polls/get/displayname'), {username: username}, function(data) { |
||
| 270 | li.appendChild(document.createTextNode(username + " (" + data + ")")); |
||
| 271 | list.appendChild(li); |
||
| 272 | }); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | var chosenDates = document.getElementById('chosenDates').value; |
||
| 279 | var chosen = ''; |
||
| 280 | if (chosenDates.length > 0) { |
||
| 281 | chosen = JSON.parse(chosenDates); |
||
| 282 | } |
||
| 283 | var text = document.getElementById('text'); |
||
| 284 | var event = document.getElementById('event'); |
||
| 285 | if (event.checked) { |
||
| 286 | chosen_type = event.value; |
||
| 287 | if (chosenDates.length > 0) { |
||
| 288 | g_chosen_datetimes = chosen; |
||
| 289 | } |
||
| 290 | for (i=0; i<chosen.length; i++) { |
||
| 291 | var date = new Date(chosen[i]*1000); |
||
| 292 | var year = date.getFullYear(); |
||
| 293 | var month = date.getMonth(); |
||
| 294 | var day = date.getDate(); |
||
| 295 | var newDate = new Date(year, month, day).getTime(); //save timestamp without time of day |
||
| 296 | month = '0' + (month+1); //month is 0-11, so +1 |
||
| 297 | day = '0' + day; |
||
| 298 | var dateStr = day.substr(-2) + '.' + month.substr(-2) + '.' + year; |
||
| 299 | var hours = date.getHours(); |
||
| 300 | var minutes = date.getMinutes(); |
||
| 301 | var ms = (hours * 60 * 60 * 1000) + (minutes * 60 * 1000); //time of day in milliseconds |
||
| 302 | hours = '0' + hours; |
||
| 303 | minutes = '0' + minutes; |
||
| 304 | var timeStr = hours.substr(-2) + ':' + minutes.substr(-2); |
||
| 305 | addRowToList(newDate/1000, dateStr, ms/1000); |
||
| 306 | addColToList(ms/1000, timeStr, newDate/1000); |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | chosen_type = text.value; |
||
| 310 | if (chosenDates.length > 0) { |
||
| 311 | g_chosen_texts = chosen; |
||
| 312 | } |
||
| 313 | for (i=0; i<chosen.length; i++) { |
||
| 314 | insertText(chosen[i], true); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | var expirepicker = jQuery('#id_expire_date').datetimepicker({ |
||
| 319 | inline: false, |
||
| 320 | onSelectDate: function(date) { |
||
| 321 | var year = date.getFullYear(); |
||
| 322 | var month = date.getMonth(); |
||
| 323 | var day = date.getDate(); |
||
| 324 | var newDate = new Date(year, month, day).getTime()/1000; |
||
| 325 | document.getElementById('expireTs').value = newDate; |
||
| 326 | }, |
||
| 327 | timepicker: false, |
||
| 328 | format: 'd.m.Y' |
||
| 329 | }); |
||
| 330 | |||
| 331 | var datepicker = jQuery('#datetimepicker').datetimepicker({ |
||
| 332 | inline:true, |
||
| 333 | step: 15, |
||
| 334 | todayButton: true, |
||
| 335 | onSelectDate: function(date) { |
||
| 336 | var year = date.getFullYear(); |
||
| 337 | var month = date.getMonth(); |
||
| 338 | var day = date.getDate(); |
||
| 339 | var newDate = new Date(year, month, day).getTime(); //save timestamp without time of day |
||
| 340 | month = '0' + (month+1); //month is 0-11, so +1 |
||
| 341 | day = '0' + day; |
||
| 342 | var dateStr = day.substr(-2) + '.' + month.substr(-2) + '.' + year; |
||
| 343 | addRowToList(newDate/1000, dateStr); |
||
| 344 | }, |
||
| 345 | onSelectTime: function(date) { |
||
| 346 | var hours = date.getHours(); |
||
| 347 | var minutes = date.getMinutes(); |
||
| 348 | var ms = (hours * 60 * 60 * 1000) + (minutes * 60 * 1000); //time of day in milliseconds |
||
| 349 | hours = '0' + hours; |
||
| 350 | minutes = '0' + minutes; |
||
| 351 | var timeStr = hours.substr(-2) + ':' + minutes.substr(-2); |
||
| 352 | addColToList(ms/1000, timeStr); |
||
| 353 | } |
||
| 354 | }); |
||
| 355 | |||
| 356 | $(document).on('click', '.date-row', function() { |
||
| 357 | var tr = $(this).parent(); |
||
| 358 | var dateId = parseInt(tr.attr('id')); |
||
| 359 | var index = tr.index(); |
||
| 360 | var cells = tr[0].cells; //convert jQuery object to DOM |
||
| 361 | for (var i=1; i<cells.length; i++) { |
||
| 362 | var cell = cells[i]; |
||
| 363 | var delIndex = g_chosen_datetimes.indexOf(dateId + parseInt(cell.id)); |
||
| 364 | if (delIndex > -1) { |
||
| 365 | g_chosen_datetimes.splice(delIndex, 1); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | var table = document.getElementById('selected-dates-table'); |
||
| 369 | table.deleteRow(index); |
||
| 370 | }); |
||
| 371 | |||
| 372 | $(document).on('click', '.date-col', function() { |
||
| 373 | var cellIndex = $(this).index(); |
||
| 374 | var timeId = parseInt($(this).attr('id')); |
||
| 375 | var table = document.getElementById('selected-dates-table'); |
||
| 376 | var rows = table.rows; |
||
| 377 | rows[0].deleteCell(cellIndex); |
||
| 378 | for (var i=1; i<rows.length; i++) { |
||
| 379 | var row = rows[i]; |
||
| 380 | var delIndex = g_chosen_datetimes.indexOf(parseInt(row.id) + timeId); |
||
| 381 | if (delIndex > -1) { |
||
| 382 | g_chosen_datetimes.splice(delIndex, 1); |
||
| 383 | } |
||
| 384 | row.deleteCell(cellIndex); |
||
| 385 | } |
||
| 386 | }); |
||
| 387 | |||
| 388 | $(document).on('click', '.text-row', function() { |
||
| 389 | var tr = $(this).parent(); |
||
| 390 | var rowIndex = tr.index(); |
||
| 391 | var name = $(this).html(); |
||
| 392 | var delIndex = g_chosen_texts.indexOf(name); |
||
| 393 | if (delIndex > -1) { |
||
| 394 | g_chosen_texts.splice(delIndex, 1); |
||
| 395 | } |
||
| 396 | var table = document.getElementById('selected-texts-table'); |
||
| 397 | table.deleteRow(rowIndex); |
||
| 398 | }); |
||
| 399 | |||
| 400 | $(document).on('click', '.icon-close', function() { |
||
| 401 | selectItem($(this)); |
||
| 402 | }); |
||
| 403 | |||
| 404 | $(document).on('click', '.icon-checkmark', function() { |
||
| 405 | deselectItem($(this)); |
||
| 406 | }); |
||
| 407 | |||
| 408 | $(document).on('click', '#text-submit', function() { |
||
| 409 | var text = document.getElementById('text-title'); |
||
| 410 | if (text.value.length === 0) { |
||
| 411 | alert('Please enter a text!'); |
||
| 412 | return false; |
||
| 413 | } |
||
| 414 | insertText(text.value); |
||
| 415 | text.value = ''; |
||
| 416 | }); |
||
| 417 | |||
| 418 | $(document).on('click', '.cl_item', function() { |
||
| 419 | var index; |
||
| 420 | var list = document.getElementById('selected-search-list-id'); |
||
| 421 | var isGroup = $(this).hasClass('is-group'); |
||
| 422 | if ($(this).hasClass('selected')) { |
||
| 423 | if (isGroup) { |
||
| 424 | index = g_chosen_groups.indexOf(this.id); |
||
| 425 | } |
||
| 426 | else { |
||
| 427 | index = g_chosen_users.indexOf(this.id); |
||
| 428 | } |
||
| 429 | if (index > -1) { |
||
| 430 | if (isGroup) { |
||
| 431 | g_chosen_groups.splice(index, 1); |
||
| 432 | } |
||
| 433 | else { |
||
| 434 | g_chosen_users.splice(index, 1); |
||
| 435 | } |
||
| 436 | $(this).remove(); |
||
| 437 | } |
||
| 438 | } else { |
||
| 439 | if (!isGroup) { |
||
| 440 | var text = this.id.replace('user_', ''); |
||
| 441 | g_chosen_users.push(this.id); |
||
| 442 | } else { |
||
| 443 | g_chosen_groups.push(this.id); |
||
| 444 | } |
||
| 445 | document.getElementById('user-group-search-box').value = ''; |
||
| 446 | var li = document.createElement('li'); |
||
| 447 | li.id = this.id; |
||
| 448 | li.className = 'cl_item cl_access_item selected' + (isGroup ? ' is-group' : ''); |
||
| 449 | if (!isGroup) { |
||
| 450 | $.post(OC.generateUrl('/apps/polls/get/displayname'), {username: text}, function(data) { |
||
| 451 | li.appendChild(document.createTextNode(text + " (" + data + ")")); |
||
| 452 | list.appendChild(li); |
||
| 453 | }); |
||
| 454 | } else { |
||
| 455 | li.appendChild(document.createTextNode($(this).html())); |
||
| 456 | list.appendChild(li); |
||
| 457 | } |
||
| 458 | $(this).remove(); |
||
| 459 | } |
||
| 460 | }); |
||
| 461 | |||
| 462 | $('.toggleable-row').hover( |
||
| 463 | function() { |
||
| 464 | var td = this.insertCell(-1); |
||
| 465 | td.className = 'toggle-all selected-all'; |
||
| 466 | }, function() { |
||
| 467 | $(this).find('td:last-child').remove(); |
||
| 468 | } |
||
| 469 | ); |
||
| 470 | |||
| 471 | $(document).on('click', '.toggle-all', function() { |
||
| 472 | var children; |
||
| 473 | var i; |
||
| 474 | if ($(this).attr('class').indexOf('selected-all') > -1) { |
||
| 475 | children = $(this).parent().children('.icon-checkmark'); |
||
| 476 | for (i=0; i<children.length; i++) { |
||
| 477 | deselectItem($(children[i])); |
||
| 478 | } |
||
| 479 | $(this).removeClass('selected-all'); |
||
| 480 | $(this).addClass('selected-none'); |
||
| 481 | } else { |
||
| 482 | children = $(this).parent().children('.icon-close'); |
||
| 483 | for (i=0; i<children.length; i++) { |
||
| 484 | selectItem($(children[i])); |
||
| 485 | } |
||
| 486 | $(this).removeClass('selected-none'); |
||
| 487 | $(this).addClass('selected-all'); |
||
| 488 | } |
||
| 489 | }); |
||
| 490 | |||
| 491 | $('input[type=radio][name=pollType]').change(function() { |
||
| 492 | if (this.value === 'event') { |
||
| 493 | chosen_type = 'event'; |
||
| 494 | document.getElementById('text-select-container').style.display = 'none'; |
||
| 495 | document.getElementById('date-select-container').style.display = 'inline'; |
||
| 496 | } else { |
||
| 497 | chosen_type = 'text'; |
||
| 498 | document.getElementById('text-select-container').style.display = 'inline'; |
||
| 499 | document.getElementById('date-select-container').style.display = 'none'; |
||
| 500 | } |
||
| 501 | }); |
||
| 502 | |||
| 503 | $('input[type=radio][name=accessType]').click(function() { |
||
| 504 | access_type = this.value; |
||
| 505 | if (access_type === 'select') { |
||
| 506 | $("#access_rights").show(); |
||
| 507 | $("#selected_access").show(); |
||
| 508 | } else { |
||
| 509 | $("#access_rights").hide(); |
||
| 510 | $("#selected_access").hide(); |
||
| 511 | } |
||
| 512 | }); |
||
| 513 | |||
| 514 | $('input[type=checkbox][name=check_expire]').change(function() { |
||
| 515 | if (!$(this).is(':checked')) { |
||
| 516 | document.getElementById('expireTs').value = ''; |
||
| 517 | } |
||
| 518 | }); |
||
| 519 | |||
| 520 | $('#user-group-search-box').on('input', debounce(function() { |
||
| 521 | var ul = document.getElementById('live-search-list-id'); |
||
| 522 | while(ul.firstChild) { |
||
| 523 | ul.removeChild(ul.firstChild); |
||
| 524 | } |
||
| 525 | var val = $(this).val(); |
||
| 526 | if (val.length < 3) { |
||
| 527 | return; |
||
| 528 | } |
||
| 529 | var formData = { |
||
| 530 | searchTerm: val, |
||
| 531 | groups: JSON.stringify(g_chosen_groups), |
||
| 532 | users: JSON.stringify(g_chosen_users) |
||
| 533 | }; |
||
| 534 | $.post(OC.generateUrl('/apps/polls/search'), formData, function(data) { |
||
| 535 | for (var i=0; i<data.length; i++) { |
||
| 536 | var ug = data[i]; |
||
| 537 | var li = document.createElement('li'); |
||
| 538 | li.className = 'cl_item cl_access_item'; |
||
| 539 | if (ug.isGroup) { |
||
| 540 | li.id = 'group_' + ug.gid; |
||
| 541 | li.className += ' is-group'; |
||
| 542 | li.appendChild(document.createTextNode(ug.gid + " (group)")); |
||
| 543 | ul.appendChild(li); |
||
| 544 | } else { |
||
| 545 | li.id = 'user_' + ug.uid; |
||
| 546 | li.appendChild(document.createTextNode(ug.uid + " (" + ug.displayName + ")")); |
||
| 547 | var span = document.createElement('span'); |
||
| 548 | span.id = 'sec_name'; |
||
| 549 | span.appendChild(document.createTextNode(ug.uid)); |
||
| 550 | li.appendChild(span); |
||
| 551 | ul.appendChild(li); |
||
| 552 | } |
||
| 553 | } |
||
| 554 | }); |
||
| 555 | }, 250)); |
||
| 556 | |||
| 557 | $('.live-search-list-user li').each(function(){ |
||
| 558 | $(this).attr('data-search-term', $(this).text().toLowerCase()); |
||
| 559 | }); |
||
| 560 | |||
| 561 | $('.live-search-box-user').on('keyup', function(){ |
||
| 562 | var searchTerm = $(this).val().toLowerCase(); |
||
| 563 | $('.live-search-list-user li').each(function(){ |
||
| 564 | if ( $(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 |
||
| 565 | || searchTerm.length < 1 |
||
| 566 | ) { |
||
| 567 | $(this).show(); |
||
| 568 | } else { |
||
| 569 | $(this).hide(); |
||
| 570 | } |
||
| 571 | }); |
||
| 572 | }); |
||
| 573 | |||
| 574 | $('.live-search-list-group li').each(function(){ |
||
| 575 | $(this).attr('data-search-term', $(this).text().toLowerCase()); |
||
| 576 | }); |
||
| 577 | |||
| 578 | $('.live-search-box-group').on('keyup', function(){ |
||
| 579 | var searchTerm = $(this).val().toLowerCase(); |
||
| 580 | $('.live-search-list-group li').each(function(){ |
||
| 581 | if ( $(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 |
||
| 582 | || searchTerm.length < 1 |
||
| 583 | ) { |
||
| 584 | $(this).show(); |
||
| 585 | } else { |
||
| 586 | $(this).hide(); |
||
| 587 | } |
||
| 588 | }); |
||
| 589 | }); |
||
| 590 | |||
| 591 | var form = document.finish_poll; |
||
| 592 | var submit_finish_poll = document.getElementById('submit_finish_poll'); |
||
| 593 | if (submit_finish_poll !== null) { |
||
| 594 | submit_finish_poll.onclick = function() { |
||
| 595 | if ( g_chosen_datetimes.length === 0 |
||
| 596 | && g_chosen_texts.length === 0 |
||
| 597 | ) { |
||
| 598 | alert(t('polls', 'Nothing selected!\nClick on cells to turn them green.')); |
||
| 599 | return false; |
||
| 600 | } |
||
| 601 | if (chosen_type === 'event') { |
||
| 602 | form.elements.chosenDates.value = JSON.stringify(g_chosen_datetimes); |
||
| 603 | } |
||
| 604 | else { |
||
| 605 | form.elements.chosenDates.value = JSON.stringify(g_chosen_texts); |
||
| 606 | } |
||
| 607 | var title = document.getElementById('pollTitle'); |
||
| 608 | if ( title === null |
||
| 609 | || title.value.length === 0 |
||
| 610 | ) { |
||
| 611 | alert(t('polls', 'You must enter at least a title for the new poll.')); |
||
| 612 | return false; |
||
| 613 | } |
||
| 614 | |||
| 615 | if (access_type === 'select') { |
||
| 616 | if ( g_chosen_groups.length === 0 |
||
| 617 | && g_chosen_users === 0 |
||
| 618 | ) { |
||
| 619 | alert(t('polls', 'Please select at least one user or group!')); |
||
| 620 | return false; |
||
| 621 | } |
||
| 622 | form.elements.accessValues.value = JSON.stringify({ |
||
| 623 | groups: g_chosen_groups, |
||
| 624 | users: g_chosen_users |
||
| 625 | }); |
||
| 626 | } |
||
| 627 | form.elements.isAnonymous.value = isAnonymous; |
||
| 628 | form.elements.hideNames.value = hideNames; |
||
| 629 | form.submit(); |
||
| 630 | }; |
||
| 631 | } |
||
| 632 | }); |
||
| 633 | |||
| 634 |